package ioStream.file.processStream.characterStream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA
 * Description:转换输入流学习
 * User:22954
 * Date:2024-03-26
 * Time:0:34
 */
@SuppressWarnings("all")
public class InputStreamReader_ {
    /*
    InputStreamReader extends Reader
    InputStreamReader(InputStream in, String charsetName)  可以将字节流包装(转换)成为字符流,并指定读取的编码格式
    在读取纯文本数据时,通过转换成字符流可以提高效率,指定编码格式消除乱码问题
        编码格式----utf-8 / gbk / gb2321 / ISO8859-1
     */
    public static void main(String[] args) {
        String filePath = "f:/Test/NewFile1.txt";
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            /*
            一般将FileInputStream包装/转换成InputStreamReader,进而包装/转换成BufferedReader
             */
            isr = new InputStreamReader(new FileInputStream(filePath),"gbk");
            br = new BufferedReader(isr);
            String s = br.readLine();
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                br.close();//关闭外层流的同时,系统自动关闭所有内层流
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
